<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language

//Người tạo : NGuyễn Huỳnh Đại Thành - Team 3
//Module 	: Edit Lastest QA Module
//Chức năng	: Quản lý việc Edit các thuộc tính cho  module Lastest QA

class LastestQA_ClickchangeController extends BaseUserController{

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
	}
	
	//Chức năng show các thuộc tính hiện tại cho người dùng
	public function indexAction(){
		//Start ------- Khởi tạo Parameter
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/lastestqa');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->textLimit 		= $language->get('text_limit');
		$this->view->textTitle 		= $language->get('text_title');
		$this->view->textSort 		= $language->get('text_order');
		$this->view->textSave 		= $language->get('text_save');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		$this->view->textError3 	= $language->get('error3');
		$this->view->textError4 	= $language->get('error4');
		$this->view->textError5 	= $language->get('error5');
		$this->view->textError6 	= $language->get('error6');
		$this->view->textError7 	= $language->get('error7');
		$this->view->textError10 	= $language->get('error10');
		/*========== End Text ==============*/
		
		if(isset($_GET["url"]))
		{
			$this->view->url = $_GET["url"];
		}
		$path = APPLICATION_PATH."/widgets/lastestqa/";
		$tbContentModule = new  LastestQA_Model_ContentModule();
		$tbDefaultModule = new	LastestQA_Model_DefaultModule();
		if(isset($_SESSION['page_code']))
		{
			$page_code=$_SESSION['page_code'];//Set page_code here
		}
		else
		{
			$this->_redirect('/');
		}
		//End ------- Khởi tạo Parameter
		
		//Start ------- Kiểm Tra file xml và truyền biến qua view
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
		
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify=="false")
		{
			$this->view->error = 10 ;
			return;
		}
		
		if(isset($xmlconfig->name))
		{
			
			$module_LastQA = $tbContentModule->selectModuleByCodeInPage($xmlconfig->name,$page_code);
			if(count($module_LastQA)>0)
			{
				$this->view->sort = $module_LastQA[0]["sort_order"];
				$this->view->name = $xmlconfig->name;
				if(isset($xmlconfig->title))
				{
					if(isset($_SESSION['language']))
					{
						$language_id = $_SESSION['language'];
						$tbLanguage = new Language_Model_Language();
						$name = $tbLanguage->getNameLanguage($language_id);
						$t="title".$name['name'];
						$this->view->title = "".$xmlconfig->$t;
					}
					
					if(isset($xmlconfig->limit))
					{
						$this->view->limit = "".$xmlconfig->limit;
					}
					else 
					{
						$this->view->error = 1 ;
					}
				}
				else 
				{
					$this->view->error = 1 ;
				}
			}
			else
			{
				$module_LastQA = $tbContentModule->selectModuleByCodeInPage($xmlconfig->name,$page_code);
				if(count($module_LastQA)>0)
				{
					$this->view->sort = $module_LastProduct[0]["sort_order"];
					$this->view->name = $xmlconfig->name;
					if(isset($xmlconfig->title))
					{
						if(isset($_SESSION['language']))
						{
							$language_id = $_SESSION['language'];
							$tbLanguage = new Language_Model_Language();
							$name = $tbLanguage->getNameLanguage($language_id);
							$t="title".$name['name'];
							$this->view->title = "".$xmlconfig->$t;
						}
						if(isset($xmlconfig->limit))
						{
							$this->view->limit = "".$xmlconfig->limit;
						}
						else 
						{
							$this->view->error = 1 ;
						}
					}
					else 
					{
						$this->view->error = 1 ;
					}
				}
				else
				{
					$this->view->error = 1 ;
				}
			}
		}
		else
		{
			$this->view->error = 1 ;
		}
		//End ------- Kiểm Tra file xml và truyền biến qua view
	}
	
	//Chức năng Edit lại các thuộc tính mà người dùng đã chọn
	public function editAction(){
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/lastestqa');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		$path = APPLICATION_PATH."/widgets/lastestqa/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify=="false")
		{
			$data=-10;
			$this->_helper->json($data);
			return;	
		}
		
		if(isset($_GET["name"]))
			{
				if(isset($_SESSION['page_code']))
				{
					$page_code=$_SESSION['page_code'];//Set page_code here
				}
				else
				{
					$this->_redirect('/');
				}

				$tbContentModule = new  LastestQA_Model_ContentModule();
				$tbDefaultModule = new	LastestQA_Model_DefaultModule();
				//lấy dữ liệu từ content module
				$module_lastest  = $tbContentModule->selectModuleByCodeInPage($_GET["name"],$page_code);
				$module_type="content"; //module_type mặc định là content
				if(count($module_lastest)<=0)//không nằm trong Content
				{
					//lấy dữ liệu từ default module
					$module_lastest = $tbDefaultModule->selectModuleByCodeInPage($_GET["name"],$page_code);
					if(count($module_lastest)>0)//nằm trong default
						$module_type="default"; //module_type là default
				}
				if(count($module_lastest)>0)
				{
					if(isset($_GET["title"])&&(strlen($_GET["title"])>=5))
					{
						if(isset($_GET["limit"])&&(is_numeric($_GET["limit"]))&& $_GET["limit"]>0 )
						{
							if(isset($_GET["sort"])&&(is_numeric($_GET["sort"]))&&$_GET["sort"]>0)
							{
								$modules=1;
								if($module_lastest[0]["sort_order"] == $_GET["sort"])
								{
									$modules = 0;
								}
								else 
								{
									$content_modules = $tbContentModule->selectModuleBySortOrderInPage($_GET["sort"],$page_code);
									$default_modules = $tbDefaultModule->selectModuleBySortOrderInPage($_GET["sort"],$page_code);
									$count_modules=count($content_modules)+count($default_modules);
								}
								if($modules==0 || $count_modules<=0)
								{
									if($module_type=="content")
										$tbContentModule->updateModule($_GET["name"],$page_code,$_GET["sort"]);//Save Sort_Order in DB
									else
										$tbDefaultModule->updateModule($_GET["name"],$page_code,$_GET["sort"]);									
									
									//Start ------- Save title and limit in xml file
									$titleXML = 'title'.$name['name'];
									$xmlconfig->$titleXML = $_GET['title'];
									$xmlconfig->limit = $_GET['limit'];
									file_put_contents($path."config.xml", $xmlconfig->asXML());
									//End ------- Save title and limit in xml file
									
									$data = 190;	
									
								}
								else
								{
									$data = -5;
								}
							}
							else
							{
								$data = -4;
							}
						}
						else 
						{
							$data = -3;
						}
					}
					else 
					{
						$data = -2;
					}
				}
				else
				{
					$data = -1;
				}
			}
			else 
			{
				$data = -1;
			}
			$this->_helper->json($data); 
	}
}
